﻿using System.Collections.Generic;

namespace TLang
{
    public static class Extensions
    {
        public static bool IsEmpty<T>(this ISet<T> set)
        {
            return set.Count == 0;
        }

        public static bool IsEmpty<T>(this IList<T> list)
        {
            return list.Count == 0;
        }

        public static void RemoveAll<T>(this ISet<T> set, IEnumerable<T> items)
        {
            foreach (var item in items)
            {
                set.Remove(item);
            }
        }

        public static bool StartsWith(this string ss, string s, int offset)
        {
            if (ss.Length <= offset 
                || ss.Length < s.Length 
                || (ss.Length - offset) < s.Length)
            {
                return false;
            }
            for (int i = offset; i < s.Length + offset; i++)
            {
                if (ss[i] != s[i - offset])
                {
                    return false;
                }
            }
            return true;
        }
    }

    public class Map<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public TValue Get(TKey key)
        {
            this.TryGetValue(key, out TValue value);

            return value;
        }

        public void Put(TKey key, TValue value)
        {
            if (this.ContainsKey(key))
            {
                this[key] = value;
            }
            else
            {
                this.Add(key, value);
            }
        }

        public void PutAll(Map<TKey, TValue> items)
        {
            foreach (var item in items)
            {
                this.Add(item.Key, item.Value);
            }
        }

        public bool IsEmpty()
        {
            return this.Count == 0;
        }
    }
}
